package com.heima.admin.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.ChannelMapper;
import com.heima.admin.service.ChannelService;
import com.heima.common.exception.CustException;
import com.heima.common.exception.CustomException;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


@Service
@Slf4j
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, AdChannel> implements ChannelService {
    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {
        //检查参数
        if (dto == null) {
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        //封装参数
        //分页参数
        IPage pageReq = new Page(dto.getPage(),dto.getSize());
        //条件参数
        LambdaQueryWrapper<AdChannel> queryWrapper = Wrappers.lambdaQuery();

        if (StringUtils.isNotBlank(dto.getName())){
            queryWrapper.like(AdChannel::getName,dto.getName());
        }
        //执行查询
        IPage<AdChannel> pageResult = this.page(pageReq,queryWrapper);
        //封装相应结果
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),pageResult.getTotal());
        responseResult.setData(pageResult.getRecords());//分页结果
        return responseResult;
    }

    @Override
    public ResponseResult insert(AdChannel channel) {
        //检查参数
        if (channel == null || StringUtils.isBlank(channel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称不能为空");
        }
        //检查数据库中是否存在
        int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, channel.getName()));
        if (count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该频道已存在");
        }
        //保存频道信息
         channel.setCreatedTime(new Date());
        this.save(channel);
        //返回结果
        return ResponseResult.okResult();

    }

    @Override
    public ResponseResult update(AdChannel channel) {
        //检查参数  （id）
        if (channel == null || channel.getId()==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道id不能为空");
        }

        //根据id查询频道信息
        AdChannel byId = this.getById(channel.getId());

        if (byId==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道信息不存在");
        }
        //如果频道名称不为空，且输入的频道名称和原来的不一致，检查是否已存在
        if (StringUtils.isNotBlank(channel.getName()) && !channel.getName().equals(byId.getName())) {
            int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, channel.getName()));
            if (count>0) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该频道名称已存在");
            }
        }
        //修改频道信息
        this.updateById(channel);
        //响应结果
        return ResponseResult.okResult();
    }

    @Transactional
    @Override
    public ResponseResult delete(Integer id) {
        if (id == 1) {
           // throw new CustomException(AppHttpCodeEnum.PARAM_INVALID,"id 不能等于1");
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"id不能等于1");
        }
        //检查参数
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道id不能为空");
        }
        //根据Id查询频道数据
        AdChannel channel = this.getById(id);
        if (channel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //检查频道状态是否启用
        if (channel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_ALLOW,"当前频道为启用状态，不可删除");
        }
        //删除频道
        this.removeById(id);
        //返回
        return ResponseResult.okResult();
    }
}

